<?php
/*----------------------------------------------------------------------
 * 后台用户管理
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Admin\Controller;

use Think\Controller;
class UserController extends AdminController {
    
    //用户列表
    public function userList(){
        
        $userLogic = new \Common\Logic\UserLogic();
        
        $option = I('request.option', '');
        
        $params = array();
        $params['group_id'] = $group_id = I('request.group_id');
        $params['keywords'] = $keywords = I('request.keywords', '', 'strip_tags');
        $params['page_size'] = 20;
        //ajax请求用户组列表
        if('ajax' == $option) {
            $params['page_size'] = 1000;
        }
        $data_list_data = $userLogic->getUserList($params);
        
        if('ajax' == $option) {
            $this->ajaxReturn($data_list_data['data_list']);
        }
        
        if(IS_POST) {
            //判断是启用还是禁用
            $option = I('post.option');
            $status = 2;
            if('go' == $option) {
                $status = 1;
            }
            
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            $user_ids = I('post.data_ids');
            foreach($user_ids as $key => $value) {
                $params = array();
                $params['user_id'] = $value;
                $params['status'] = $status;
                $params['admin_user_id'] = $user_info['user_id'];
                $result = $userLogic->update($params);
            }
            $this->success($result['status_message'], C('REFERER'), 3);
        }
        
        //获取用户组选项
        $user_group_options = $userLogic->getUserGroupOptions(array('empty'=>1));
        
        $page_title = '用户列表';
        $this->assign(get_defined_vars());
        $this->display('userList');
    }
    
    //添加用户
    public function add(){
        
        $userLogic = new \Common\Logic\UserLogic();
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
            $params['user_name'] = I('post.user_name' ,'', 'strip_tags');
            $params['nick_name'] = I('post.nick_name' ,'', 'strip_tags');
            $params['mobile'] = I('post.mobile' ,'', 'strip_tags');
            $params['group_id'] = I('post.group_id');
            $params['password'] = I('post.password' ,'', 'strip_tags');
            $params['platform'] = 1;
            $params['admin_user_id'] = $user_info['user_id'];
            $result = $userLogic->add($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], U('User/userList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        //获取用户组选项
        $user_group_options = $userLogic->getUserGroupOptions();
        
        $page_title = '添加用户';
        $this->assign(get_defined_vars());
        $this->display('user');
    }
    
    //编辑用户
    public function edit(){
        
        $userLogic = new \Common\Logic\UserLogic();
        $user_id = I('request.user_id');
        
        if(IS_POST) {
            //当前用户信息
            $user_info = $userLogic->getCurrentUserInfo();
            
            $params = array();
            $params['user_name'] = I('post.user_name' ,'', 'strip_tags');
            $params['nick_name'] = I('post.nick_name' ,'', 'strip_tags');
            $params['mobile'] = I('post.mobile' ,'', 'strip_tags');
            $params['group_id'] = I('post.group_id');
            $params['password'] = I('post.password' ,'', 'strip_tags');
            $params['admin_user_id'] = $user_info['user_id'];
            $params['user_id'] = $user_id;
            $result = $userLogic->edit($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], U('User/userList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        //获取用户组选项
        $user_group_options = $userLogic->getUserGroupOptions();
        
        //查询当前用户
        $model = $userLogic->find($user_id);
        
        $page_title = '编辑用户';
        $this->assign(get_defined_vars());
        $this->display('user');
    }
    
    //更新用户
    public function update(){
        
        $userLogic = new \Common\Logic\UserLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        $params = array();
        $params['user_id'] = I('request.user_id');
        $status = I('request.status');
        if($status) {
            $params['status'] = $status;
        }
        $params['admin_user_id'] = $user_info['user_id'];
        $result = $userLogic->update($params);
        if(1 == $result['status_code']) {
            $this->success($result['status_message'], C('REFERER'), 3);
        } else {
            $this->error($result['status_message'], C('REFERER'), 3);
        }
    }
    
    //用户组列表
    public function userGroupList(){
        
        $userLogic = new \Common\Logic\UserLogic();
        
        $params = array();
        $params['order'] = $order = I('get.order', 'user_count_desc', 'strip_tags');
        $params['page_size'] = 20;
        $data_list_data = $userLogic->getUserGroupList($params);
        
        $page_title = '角色列表';
        $this->assign(get_defined_vars());
        $this->display('userGroupList');
    }
    
    //添加用户组
    public function addGroup(){
        
        $userLogic = new \Common\Logic\UserLogic();
        $menuLogic = new \Common\Logic\MenuLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        if(IS_POST) {
            $params = array();
            $params['group_name'] = I('post.group_name' ,'', 'strip_tags');
            $params['description'] = I('post.description' ,'', 'strip_tags');
            $params['is_front_login'] = I('post.is_front_login' , 2);
            $params['menu_ids'] = I('post.menu_ids');
            $params['user_id'] = $user_info['user_id'];
            $result = $userLogic->addGroup($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], U('User/userGroupList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        //获取权限菜单列表
        $params = array();
        $params['parent_id'] = 0;
        $menu_data_list = $menuLogic->getMultMenuList($params);
        $menu_list = $menuLogic->getClearMultMenuList($menu_data_list, 0);
        
        //模板展示参数准备
        $yes_no_options = getYesOrNo();
        
        $page_title = '添加角色';
        $this->assign(get_defined_vars());
        $this->display('group');
    }
    
    //编辑用户组
    public function editGroup(){
          
        $userLogic = new \Common\Logic\UserLogic();
        $menuLogic = new \Common\Logic\MenuLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        $group_id = I('request.group_id');
        if(IS_POST) {
            $params = array();
            $params['group_name'] = I('post.group_name' ,'', 'strip_tags');
            $params['description'] = I('post.description' ,'', 'strip_tags');
            $params['is_front_login'] = I('post.is_front_login' , 2);
            $params['menu_ids'] = I('post.menu_ids');
            $params['group_id'] = $group_id;
            $params['user_id'] = $user_info['user_id'];
            $result = $userLogic->editGroup($params);
            if(1 == $result['status_code']) {
                $this->success($result['status_message'], U('User/userGroupList'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        //查询当前用户组
        $model = $userLogic->findGroup($group_id);
        
        //获取权限菜单列表
        $params = array();
        $params['parent_id'] = 0;
        $menu_data_list = $menuLogic->getMultMenuList($params);
        $menu_list = $menuLogic->getClearMultMenuList($menu_data_list, 0);
        
        //模板展示参数准备
        $yes_no_options = getYesOrNo();
        
        $page_title = '编辑角色';
        $this->assign(get_defined_vars());
        $this->display('group');
    }
    
    //删除用户组
    public function deleteGroup(){
        
        $userLogic = new \Common\Logic\UserLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        $params = array();
        $params['group_id'] = I('request.group_id');
        $params['user_id'] = $user_info['user_id'];
        $result = $userLogic->deleteGroup($params);
        if(1 == $result['status_code']) {
            $this->success($result['status_message'], C('REFERER'), 3);
        } else {
            $this->error($result['status_message'], C('REFERER'), 3);
        }
    }
    
    //个人资料
    public function baseinfo(){
        $userLogic = new \Common\Logic\UserLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        $option = I('request.option', '');
        
        if(IS_POST) {
            $params = array();
            $params['nick_name'] = I('post.nick_name', '', 'strip_tags');
            $params['mobile'] = I('post.mobile', '', 'strip_tags');
            $params['user_id'] = $user_info['user_id'];
            $params['admin_user_id'] = $user_info['user_id'];
            $result = $userLogic->update($params);
            if(1 == $result['status_code']) {
                $result['status_message'] = '资料修改成功，即将自动返回个人资料。';
                $this->success($result['status_message'], U('User/baseinfo'), 3);
            } else {
                $this->error($result['status_message'], U('User/baseinfo').'/option/baseinfoForm', 3);
            }
        }
        
        //获取用户组选项
        $user_group_options = $userLogic->getUserGroupOptions();
        
        //当前用户信息
        $model = $userLogic->find($user_info['user_id']);
        
        $page_title = '个人资料';
        $this->assign(get_defined_vars());
        $this->display('baseinfo');
    }
    
    //修改密码
    public function password(){
        $userLogic = new \Common\Logic\UserLogic();
        
        //当前用户信息
        $user_info = $userLogic->getCurrentUserInfo();
        
        if(IS_POST) {
            $params = array();
            $params['password'] = I('post.password', '', 'strip_tags');
            $params['new_password'] = I('post.new_password', '', 'strip_tags');
            $params['user_id'] = $user_info['user_id'];
            $params['admin_user_id'] = $user_info['user_id'];
            $result = $userLogic->update($params);
            if(1 == $result['status_code']) {
                $result['status_message'] = '密码修改成功，即将刷新页面。';
                $this->success($result['status_message'], C('REFERER'), 3);
            } else {
                $this->error($result['status_message'], C('REFERER'), 3);
            }
        }
        
        $page_title = '修改密码';
        $this->assign(get_defined_vars());
        $this->display('password');
    }
    
    //系统消息
    public function systemMessage()
    {
        //获取系统消息
        $messageLogic = new \Common\Logic\MessageLogic();
        $params = array();
        $params['page_size'] = C('LIST_ROWS');
        $data_list_data = $messageLogic->getSystemMessageList($params);
        
        $page_title = '系统消息';
        $this->assign(get_defined_vars());
        $this->display('systemMessageList');
    }
}